<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="list" stripe>
        <el-table-column
          prop="stem"
          label="标题"
          width="350px"
        ></el-table-column>
        <el-table-column prop="creator" label="作者"></el-table-column>
        <el-table-column prop="likeCount" label="点赞"></el-table-column>
        <el-table-column prop="views" label="浏览数"></el-table-column>
        <el-table-column
          prop="createdAt"
          label="更新时间"
          width="180px"
        ></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="openDrawer('view', row.id)"></i>
              <i
                class="el-icon-edit-outline"
                @click="openDrawer('edit', row.id)"
              ></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout=" prev, pager, next, total"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="pageSize"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 下边是抽屉盒子 -->
    <el-drawer
      :title="drawerTitle"
      :visible.sync="isShowDrawer"
      :before-close="handleClose"
      size="50%"
    >
      <!-- 这个是预览盒子 -->
      <div v-if="drawerType === 'view'">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>
      <el-form
        v-else
        ref="form"
        label-width="80px"
        :model="form"
        :rules="rules"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onAdd">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {
  articleListAPI,
  createArticleAPI,
  removeArticleAPI,
  getArticleDetail,
  updataArticleAPI
} from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      list: [], // 准备存储接口返回的列表数据
      total: 0, // 准备存储数据总数
      current: 1, // 页码 默认获取第一页的数据
      pageSize: 10, // 每页显示10条数据
      isShowDrawer: false, // 准备数据项控制抽屉开关
      drawerType: '', // 添加、修改、预览等类型
      form: {
        // 表单验证
        stem: '',
        content: ''
      },
      rules: {
        // 表单验证
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 调用方法发送获取数据的 ajax请求
    async getList () {
      const res = await articleListAPI({
        current: this.current,
        pageSize: this.pageSize
      })
      // console.log(res) // 响应结果回来给数据项赋值
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 分页的时候需要调用此方法
    handleCurrentChange (val) {
      // console.log(val)
      this.current = val
      this.getList()
    },
    // 抽屉关闭
    handleClose () {
      this.isShowDrawer = false
      this.$refs.form?.resetFields() // 重置表单  ?.是新语法,如果前边是true 则才会调用后边的方法
      this.form.stem = this.form.content = ''
      // console.log('抽屉关闭了')
    },
    // 抽屉打开
    async openDrawer (type, id) {
      this.isShowDrawer = true // 让抽屉打开
      this.drawerType = type // 更改类型
      if (type !== 'add') {
        const res = await getArticleDetail(id)
        this.form = {
          ...res.data
        }
      }
    },
    // 添加面经方法  这里还要进行完整验证
    onAdd () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // 无论是添加和修改都需要完整验证 所以下面需要判断
          if (this.drawerType === 'add') {
            const { stem, content } = this.form // 结构赋值
            await createArticleAPI({ stem, content }) // 后边是{stem : stem,.....} 的形式
            this.$message.success('添加成功')
            this.current = 1 // 因为添加后  无论当时看的第几页 都要重置到第一页开始看
          } else {
            const { stem, content, id } = this.form
            await updataArticleAPI({ stem, content, id })
            this.$message.success('修改成功')
          }

          this.handleClose() // 添加成功后关闭抽屉 调方法就行
          this.getList() // 重新获取数据
        }
      })
    },
    // 删除一条面经
    del (id) {
      this.$confirm('你确定要删除吗?', '提示')
        .then(async () => {
          // 点了确认做什么
          await removeArticleAPI(id) // 调接口
          this.getList() // 重新获取数据
          this.$message.success('删除成功')
        })
        .catch(() => {
          // 点了取消做什么
        })
    }
  },
  // 利用计算属性来控制  抽屉的标题
  computed: {
    drawerTitle () {
      let title = '大标题'
      if (this.drawerType === 'add') title = '添加面经'
      if (this.drawerType === 'view') title = '面经预览'
      if (this.drawerType === 'edit') title = '修改面经'
      return title
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
